
#include "ui_page.h"

static void gif_m0_task(lv_task_t *task);
static void gif_m4_task(lv_task_t *task);

/**
 * @brief	跳转 M0 / M4 页
 * @param 	parent 		- 页面父节点
 *			user_data 	- 用户自定义数据
 * @retval	obj 		- 页面光标
 */
lv_obj_t *page_jump_m0_m4(lv_obj_t *parent, void *user_data)
{
	lv_tabview_set_tab_act(TabView, *(uint16_t *)user_data, LV_ANIM_OFF);
	return Page_Table[*(uint16_t *)user_data].obj;
}

/**
 * @brief	创建 GIF 页 - M0 / M4
 * @param 	parent 		- 页面父节点
 *			user_data 	- 用户自定义数据
 * @retval	obj 		- 页面光标
 */
lv_obj_t *page_jump_gif(lv_obj_t *parent, void *user_data)
{
	// background_setting
	lv_obj_t *background = lv_obj_create(parent, NULL);
	lv_obj_set_size(background, LV_HOR_RES_MAX, LV_VER_RES_MAX);
	lv_obj_set_style_local_bg_color(background, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_BLACK); //黑色背景
	lv_obj_set_style_local_bg_opa(background, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_COVER);	 //透明-LV_OPA_TRANSP      不透明-LV_OPA_COVER
	lv_obj_set_style_local_border_width(background, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, 0);
	lv_obj_set_style_local_radius(background, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, 0);

#if (LCD_HDOT < 800 && LCD_VDOT <= 480)
	if (0 == load_to_sdram_source(SPI, img, Image_Hash_Table[IMG_WALLPAPER].file.name, Image_Hash_Table[IMG_WALLPAPER].img))
	{
		//使用重复加载区域重载背景
		lv_obj_set_style_local_pattern_image(background, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, &IMG_Wallpaper);

		//需要在退出页面时释放
		// sdram_free((void *)Image_Hash_Table[IMG_WALLPAPER].img->data);
	}
#endif //( LCD_HDOT < 800 && LCD_VDOT <= 480 )

	// switch_setting
	lv_obj_t *sw = lv_switch_create(background, NULL);
	lv_switch_set_anim_time(sw, 0);						   	   //动画时长 100
	lv_switch_off(sw, LV_ANIM_ON);							   //默认关闭, 开启动画
	lv_obj_set_size(sw, ICON_HB_W, ICON_HB_H);				   //设置绝对大小
	lv_obj_align(sw, NULL, LV_ALIGN_IN_TOP_MID, 0, ICON_HB_H); //相对父级顶部居中对齐
	lv_obj_set_event_cb(sw, event_cb_video_switch);

	// slider_setting
	lv_obj_t *slider = lv_slider_create(background, NULL);
	lv_slider_set_range(slider, 1, 100);					//范围1~100
	lv_slider_set_anim_time(slider, 0);						//设置初值时的动画时长
	lv_slider_set_value(slider, 100, LV_ANIM_OFF);			//初值100, 不用动画效果
	lv_slider_set_type(slider, LV_SLIDER_TYPE_NORMAL);		//普通类型
	lv_obj_set_adv_hittest(slider, true);					//仅拖动有效，禁用滑块点击
	lv_obj_set_size(slider, GIF_M0_M4_W, GIF_M0_M4_H / 14); //设置绝对大小

	lv_obj_align(slider, NULL, LV_ALIGN_IN_BOTTOM_MID, 0, -GIF_M0_M4_H / 10); //相对父级底部居中对齐
	lv_obj_set_event_cb(slider, event_cb_video_slider);

	if (parent == Page_Table[M0].obj || parent == Page_Table[M4].obj) //判断是从哪个页面进入
	{
		uint16_t page_index = (parent == Page_Table[M0].obj) ? GIF_M0 : GIF_M4;
		Page_Table[page_index].obj = background; //记录保存新建页面父节点

		// icon_setting
		page_create(background, Page_Table[page_index].widget, Page_Table[page_index].widget_size);

		// gif_setting
		GIF.obj = lv_img_create(background, NULL);
		GIF.index = 0;
		GIF.task = lv_task_create((GIF_M0 == page_index) ? gif_m0_task : gif_m4_task, M0_M4_TASK_MS, LV_TASK_PRIO_MID, user_data);
		lv_task_ready(GIF.task);
	}
	// video_setting
	PLAY_MV(GET_OFFSET_X(LV_HOR_RES_MAX, FLASH_VIDEO_AREA_W, 1), GET_OFFSET_Y(LV_VER_RES_MAX, FLASH_VIDEO_AREA_H, 1));

	return background;
}

/**
 * @brief	退出 GIF 页 - M0 / M4
 * @param 	parent 		- 页面父节点
 *			user_data 	- 用户自定义数据
 * @retval	obj 		- 页面光标
 */
lv_obj_t *page_quit_gif(lv_obj_t *parent, void *user_data)
{
	if (parent != Page_Table[GIF_M0].obj && parent != Page_Table[GIF_M4].obj)
		return parent; // assert

	lv_obj_del(parent); //退出页面 / 清理页面父级对象

	uint16_t page_index = (parent == Page_Table[GIF_M0].obj) ? GIF_M0 : GIF_M4;
	Page_Table[page_index].obj = NULL;			   //置空页面父结点
	page_index = (page_index == GIF_M0) ? M0 : M4; //返回上一层

	// clean_video
	Video_Touch_Flag = false;
	lcd_set_alpha(LAYER_2_VIDEO, 0xFF);
	mv_task_over();

#if (LCD_HDOT < 800 && LCD_VDOT <= 480)
	//需要在退出页面时释放
	sdram_free((void *)Image_Hash_Table[IMG_WALLPAPER].img->data);
#endif //( LCD_HDOT < 800 && LCD_VDOT <= 480 )

	if (0 == *(uint16_t *)user_data) // Home键
	{
		// switch_ui_page
		lv_tabview_set_tab_act(TabView, HOME, LV_ANIM_OFF);
		page_index = HOME;
	}
	return Page_Table[page_index].obj;
}

/**
 * @brief	刷新任务，用于刷新图片源以实现 gif 效果(用于 M0 or M4 芯片素材显示)
 * @param 	task -任务对象
 * @retval	\
 * @note 	\
 */
static void gif_m0_task(lv_task_t *task)
{
	uint16_t index = GIF.index;
	switch (*(uint16_t *)task->user_data) //在多次执行且分支数更多时 switch 比 if / else 速度更快
	{
	case (SWM_160): //素材缺失
	{
		index += M0_GIF_181_START;
		GIF.img = GIF_M0_181;
		GIF.index_max = M0_GIF_181_SIZE;
		break;
	}
	case (SWM_181):
	{
		index += M0_GIF_181_START;
		GIF.img = GIF_M0_181;
		GIF.index_max = M0_GIF_181_SIZE;
		break;
	}
	case (SWM_190):
	{
		index += M0_GIF_190_START;
		GIF.img = GIF_M0_190;
		GIF.index_max = M0_GIF_190_SIZE;
		break;
	}
	case (SWM_201):
	{
		index += M0_GIF_201_START;
		GIF.img = GIF_M0_201;
		GIF.index_max = M0_GIF_201_SIZE;
		break;
	}
	case (SWM_220): //素材缺失
	{
		index += M0_GIF_181_START;
		GIF.img = GIF_M0_181;
		GIF.index_max = M0_GIF_181_SIZE;
		break;
	}
	case (SWM_260):
	{
		index += M0_GIF_260_START;
		GIF.img = GIF_M0_260;
		GIF.index_max = M0_GIF_260_SIZE;
		break;
	}
	default:
		break; //此处应抛出错误并显示
	}

	if (0 == load_to_sdram_source(SPI, img, Image_Hash_Table[index].file.name, Image_Hash_Table[index].img))
	{
		lv_img_set_src(GIF.obj, &GIF.img[GIF.index++]); // 从 spi->sdram 加载图片，加载一张显示一张
		GIF.index = (GIF.index < GIF.index_max) ? GIF.index : 0;

		LOG_INFO("[sdram_free] addr = 0x[%p]\r\n", Image_Hash_Table[index].img->data);
		sdram_free((void *)Image_Hash_Table[index].img->data);
	}
	lv_obj_align(GIF.obj, NULL, LV_ALIGN_CENTER, 0, 0); //相对父级居中对齐
}

/**
 * @brief	刷新任务，用于刷新图片源以实现 gif 效果(用于 M0 or M4 芯片素材显示)
 * @param 	task -任务对象
 * @retval	\
 * @note 	\
 */
static void gif_m4_task(lv_task_t *task)
{
	uint16_t index = GIF.index;
	switch (*(uint16_t *)task->user_data)
	{
	case (SWM_320):
	{
		index += M4_GIF_320_START;
		GIF.img = GIF_M4_320;
		GIF.index_max = M4_GIF_320_SIZE;
		break;
	}
	case (SWM_341): //素材缺失
	{
		index += M4_GIF_320_START;
		GIF.img = GIF_M4_320;
		GIF.index_max = M4_GIF_320_SIZE;
		break;
	}
	default:
		break; //此处应抛出错误并显示
	}

	if (0 == load_to_sdram_source(SPI, img, Image_Hash_Table[index].file.name, Image_Hash_Table[index].img))
	{
		lv_img_set_src(GIF.obj, &GIF.img[GIF.index++]); // 从 spi->sdram 加载图片，加载一张显示一张
		GIF.index = (GIF.index < GIF.index_max) ? GIF.index : 0;

		LOG_INFO("[sdram_free] addr = 0x[%p]\r\n", Image_Hash_Table[index].img->data);
		sdram_free((void *)Image_Hash_Table[index].img->data);
	}

	lv_obj_align(GIF.obj, NULL, LV_ALIGN_CENTER, 0, 0); //相对父级居中对齐
}
